package xyz.iguilty.workflow.enums;

import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;
import xyz.iguilty.workflow.anno.CirculationVariable;
import xyz.iguilty.workflow.core.service.AbstractActivity;
import xyz.iguilty.workflow.model.DealResult;
import xyz.iguilty.workflow.model.TaskForm;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 流程处理对策
 */
public enum ProcessAction {
    START("00","发起"),
    RESTART("01","重新发起"),
    PASS("10","通过"),
    ROLLBACK("20","回退"),
    UPGRADE("30","升级"),
    JUMP("40","跳过"),
    DEAL("50", "处理"),
    DELETE("90","删除"),
    RECOVERY("91","回收"),
    REJECT("92","拒绝"),
    DELEGATE("93", "委派"),
    TURN("94", "转办"),
    CLAIM("95", "签收"),
    SUSPEND("96", "挂起"),
    ACTIVE("97", "激活"),
    END("99", "终止"),
    OTHER("100", "其它"),
    ;

    /**
     * 类型
     */
    @Getter@Setter
    private String action;
    /**
     * 描述
     */
    @Getter
    private final String remark;

    ProcessAction(String action, String remark) {
        this.action = action;
        this.remark = remark;
    }

    /**
     * 根据action的值获得对应的枚举对象
     * @param action
     * @return
     */
    public static ProcessAction getByActionCode(String action){
        if (StringUtils.isEmpty(action)){
            return ProcessAction.DEAL;
        }
        ProcessAction[] values = ProcessAction.values();
        for (ProcessAction processAction:values){
            if (action.equals(processAction.getAction())){
                return processAction;
            }
        }
        ProcessAction other = ProcessAction.OTHER;
        other.setAction(action);
        return other;
    }

    public DealResult process(AbstractActivity activity, TaskForm form){
        DealResult dealResult = null;
        switch (this){
            case RESTART:
                dealResult = activity.restart(form);
                break;
            case PASS:
                dealResult = activity.pass(form);
                break;
            case ROLLBACK:
                dealResult = activity.rollback(form);
                break;
            case REJECT:
                dealResult = activity.reject(form);
                break;
            case DEAL:
                dealResult = activity.deal(form);
                break;
            case OTHER:
            default:
                Method[] methods = activity.getClass().getDeclaredMethods();
                for (Method method:methods){
                    CirculationVariable circulationVariable = method.getAnnotation(CirculationVariable.class);
                    if (circulationVariable != null&&circulationVariable.value().equals(this.getAction())) {
                        try {
                            Object result = method.invoke(activity, form);
                            dealResult = (DealResult) result;
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
                if (dealResult==null){
                    throw new IllegalArgumentException("该流转变量找不到对应的业务方法");
                }
        }
        return dealResult;
    }

}
